$(function () {
    var myAjax = new Myajax();
    var tbody = $("tbody");

    function tableOperator(users) {
        users.forEach((item, index) = > {
            tableAdd(item);
    })
        ;

    }

    function dateFormat(time) {
        var date = new Date(time);
        var month = date.getMonth() + 1;
        if (date.getMonth() + 1 < 10) {
            var month = "0" + (date.getMonth() + 1);
        }
        var da = date.getDate();
        if (date.getDate() < 10) {
            var da = "0" + date.getDate();
        }
        return date.getFullYear() + "-" + month + "-" + da;
    }

    deleteUser = function deleteUser(btn) {
        var uid = $(btn).parent().parent().find("td:eq(0)").text();
        var url = "/user/delete/" + uid;
        myAjax.sendPost(url, null, tableDelete, failFn);
    }

    updateUser = function updateUser(btn) {
        var id = $(btn).parent().parent().find("td:eq(0)").text();
        var uname = $(btn).parent().parent().find("td:eq(1)").text();
        var sex = $(btn).parent().parent().find("td:eq(2)").text();
        var age = $(btn).parent().parent().find("td:eq(3)").text();
        var sal = $(btn).parent().parent().find("td:eq(4)").text();
        var birth = $(btn).parent().parent().find("td:eq(5)").text();
        $("input[name=updateId]").val(id);
        $("input[name=updateUname]").val(uname);
        $("input[name=updateSex][value='true']").removeAttr("checked");
        $("input[name=updateSex][value='false']").removeAttr("checked");
        console.log(sex);

        //版本不能用attr
        $("input[name=updateSex][value='" + sex + "']").prop("checked", "checked");
        $("input[name=updateAge]").val(age);
        $("input[name=updateSal]").val(sal);
        $("input[name=updateBirth]").val(birth);
        $('#myModal').modal('show');
    }

    function tableAdd(item) {
        $("<tr>").append("<td>" + item.id + "</td>")
            .append("<td>" + item.uname + "</td>")
            .append("<td>" + item.sex + "</td>")
            .append("<td>" + item.age + "</td>")
            .append("<td>" + item.sal + "</td>")
            .append("<td>" + dateFormat(item.birth) + "</td>")
            .append("<td><button class='btn btn-danger' onclick='deleteUser(this)'>delete</button></td>")
            .append("<td><button class='btn btn-primary' onclick='updateUser(this)'>update</button></td>")
            .appendTo(tbody);
    }

    function tableDelete(user) {
        var trs = tbody.find("tr");
        trs.each((index, item) = > {
            if(item.firstChild.textContent == user.id
    )
        {
            item.remove();
        }
    })
    }

    function tableUpdate(user) {
        var trs = tbody.find("tr");
        trs.each((index, item) = > {
            if(item.firstChild.textContent == user.id
    )
        {
            $(item).find("td:eq(0)").text(user.id);
            $(item).find("td:eq(1)").text(user.uname);
            $(item).find("td:eq(2)").text(user.sex);
            $(item).find("td:eq(3)").text(user.age);
            $(item).find("td:eq(4)").text(user.sal);
            $(item).find("td:eq(5)").text(dateFormat(user.birth));
        }
    })
    }

    function failFn(msg) {
        console.log(msg);
    }

    myAjax.sendGet("user/all/asyn", tableOperator, failFn);
    $("#add").click(function () {
        var uname = $("input[name=uname]").val();
        var age = $("input[name=age]").val();
        var sex = $("input[name=sex]:checked").val();
        var sal = $("input[name=sal]").val();
        var birth = $("input[name=birth]").val();
        var user = {uname: uname, age: age, sex: sex, sal: sal, birth: birth};

        myAjax.sendPost("user/add/asyn", user, tableAdd, failFn);
    })

    $("#updateUser").click(function () {
        var id = $("input[name=updateId]").val();
        var uname = $("input[name=updateUname]").val();
        var sex = $("input[name=updateSex]:checked").val();
        var age = $("input[name=updateAge]").val();
        var sal = $("input[name=updateSal]").val();
        var birth = $("input[name=updateBirth]").val();
        var user = {id: id, uname: uname, age: age, sex: sex, sal: sal, birth: birth};

        $('#myModal').modal('hide');
        console.log(user);
        myAjax.sendPost("user/update/asyn", user, tableUpdate, failFn);
    })

    //$('#myModal').modal('show');
})